﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using SharedLib.Helpers;
using SharedLib.Engine;
using Lidgren.Network;
using Lidgren.Network.Xna;
using Microsoft.Xna.Framework;

namespace SharedLib.Network.Server
{
    public class ServerWorld
    {
        Server server;
        private byte[,] _world;
        public List<ServerChunk> Chunks;
        

        public byte[,] World
        {
            get { return _world; }
            set { _world = value; }
        }
        
        public ServerWorld(Server srv)
        {
            Chunks = new List<ServerChunk>();
            server = srv;

            float pourcent = 0;
            for (int x = -10; x < 10; x++)
            {
                for (int y = -10; y < 10; y++)
                {
                    ServerChunk newChunk = new ServerChunk();
                    
                    newChunk.X = x;
                    newChunk.Y = y;
                    
                    for (int i = 0; i < Constants.CHUNK_WIDTH; i++)
                    {
                        for (int j = 0; j < Constants.CHUNK_HEIGHT; j++)
                        {

                            newChunk.Tiles[i, j] = (byte)BlocType.Grass;
                            if (i == 0 || i == Constants.CHUNK_WIDTH - 1)
                            {

                                
                                if (Constants.Random.Next(0,10)>5)
                                {
                                    newChunk.Tiles[i, j] = (byte)BlocType.RoundLineSand;
                                    newChunk.Collisions[i, j] = 1;
                                }
                            }  
                            if(j == 0 || j == Constants.CHUNK_HEIGHT - 1 )
                            {

                                if (Constants.Random.Next(0, 10) > 5)
                                {

                                    newChunk.Tiles[i, j] = (byte)BlocType.RoundLineSand;
                                    newChunk.Collisions[i, j] = 1;
                                }
                           }
                      
                        }
                        
                    }

                    Chunks.Add(newChunk);
                }
                pourcent += 0.5f;
                Logger.LogColor("World generation : " + pourcent, ConsoleColor.Yellow);
            }

        }

        public void WriteWorld(ServerPlayer player, NetOutgoingMessage outmsg)
        {
            int chunkX = (int)(player.Position.X / Constants.TILE_SIZE / Constants.CHUNK_WIDTH) - (player.Position.X < 0 ? 1 : 0);
            int chunkY = (int)(player.Position.Y / Constants.TILE_SIZE / Constants.CHUNK_HEIGHT) - (player.Position.Y < 0 ? 1 : 0);

            byte[,] playerView = new byte[3 * Constants.CHUNK_WIDTH, 3 * Constants.CHUNK_HEIGHT];
            byte[,] playerCollision = new byte[3 * Constants.CHUNK_WIDTH, 3 * Constants.CHUNK_HEIGHT];


            int tmpX = 0;
            int tmpY = 0;

            for (int x = chunkX - 1; x < chunkX + 2; x++)
            {
                for (int y = chunkY - 1; y < chunkY + 2; y++)
                {
                    ServerChunk currentChunk = null;
                    currentChunk = server.GameWorld.Chunks.Where(c => c.X == x && c.Y == y).FirstOrDefault();

                    if (currentChunk != null)
                    {
                        for (int cx = 0; cx < Constants.CHUNK_WIDTH; cx++)
                        {
                            for (int cy = 0; cy < Constants.CHUNK_HEIGHT; cy++)
                            {
                                playerView[tmpX * Constants.CHUNK_WIDTH + cx, tmpY * Constants.CHUNK_HEIGHT + cy] = currentChunk.Tiles[cx, cy];
                                playerCollision[tmpX * Constants.CHUNK_WIDTH + cx, tmpY * Constants.CHUNK_HEIGHT + cy] = currentChunk.Collisions[cx, cy];

                            }
                        }
                    }

                    tmpY++;

                }
                tmpX++;
                tmpY = 0;
            }

            
            //start draw position
            outmsg.Write(new Vector2(chunkX - 1, chunkY - 1));


            for (int i = 0; i < playerView.GetLength(0); i++)
            {
                for (int j = 0; j < playerView.GetLength(1); j++)
                {
                    outmsg.Write(playerView[i, j]);
                    outmsg.Write(playerCollision[i, j]);
                }
            }
        }

        public void GenerateSpawnPosition(ServerPlayer player)
        {
            int x = 0, y = 0;
            bool stop = false;

            player.Position = new Microsoft.Xna.Framework.Vector2(-200,200);
           
        }
    }
}
